import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/manager/xindong_notice_ctr.dart';
import 'package:xindong/routes/common/xindong_help_yemian.dart';
import 'package:xindong/routes/tab_chats/xindong_notice_yemian.dart';
import 'package:xindong/routes/user/visit/xindong_visit_yemian.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/xindong_count_red_dots.dart';

import '../../../common/xindong_define.dart';

class XinDongMessageHeaderView extends StatelessWidget {
  const XinDongMessageHeaderView({super.key});

  void onSelect(XinDongMessageTypeClass model) {
    switch (model.type) {
      case XinDongMessageType.help:
        XinDongPageSingleton.instance.pushYeMian(const XinDongHelpYeMian());
        break;
      case XinDongMessageType.kefu:
        XinDongPageSingleton.instance.jumpToServiceYeMian();
        break;
      case XinDongMessageType.system:
        XinDongPageSingleton.instance.pushYeMian(const XinDongNoticeYeMian());
        break;
      case XinDongMessageType.visit:
        XinDongPageSingleton.instance.pushYeMian(const XinDongVisitYeMian());
        break;
      default:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: screenWidth,
      height: 108,
      padding: const EdgeInsets.only(left: 16, right: 16, top: 16),
      child: GetBuilder<XinDongNoticeCtr>(builder: (controller) {
        return Wrap(
            alignment: WrapAlignment.spaceBetween,
            direction: Axis.horizontal,
            children: controller.modelList.map(
              (model) {
                return _XinDongMessageHeaderGridItem(
                  model: model,
                  onTap: () => onSelect(model),
                  isSeenMe: false,
                );
              },
            ).toList());
      }),
    );
  }
}

// 消息列表item
class _XinDongMessageHeaderGridItem extends StatelessWidget {
  final XinDongMessageTypeClass model;
  final Function() onTap;
  final bool isSeenMe;

  const _XinDongMessageHeaderGridItem({Key? key, required this.model, required this.onTap, required this.isSeenMe})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.opaque,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // 图标
          SizedBox(
            height: 51,
            width: 51,
            child: Stack(
              clipBehavior: Clip.none,
              children: [
                ImageUtil.asset(model.iconName!),
                // 未读数量
                Positioned(
                  top: 0,
                  right: -8,
                  child: XinDongUnreadCountWidget(count: model.count),
                ),
                if (model.type == XinDongMessageType.visit && isSeenMe)
                  const Positioned(
                    top: 0,
                    right: 4,
                    child: XinDongUnreadCountWidget(onlyDot: true, height: 10),
                  ),
              ],
            ),
          ),
          const SizedBox(height: 6),
          Text(
            model.name,
            style: const TextStyle(color: DZColors.titleColor, fontSize: 14, fontWeight: FontWeight.normal),
          ),
        ],
      ),
    );
  }
}
